package yl.hs.bmipfull.service.report.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.material.IRawMaterialInfoDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.daos.production.IProductionRecordDao;
import yl.hs.bmipfull.daos.report.ICostingDao;
import yl.hs.bmipfull.pojo.production.ProductionRecordEntity;
import yl.hs.bmipfull.service.report.ICostingService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.production.ConsumptionModel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作用:   <br/>
 * 创建时间: 2023-05-24 14:33 <br/>
 *
 * @author 吴高丰
 */
@Service
public class CostingServiceImpl implements ICostingService {
    @Autowired
    ICostingDao costingDao;
    @Autowired
    IRawMaterialInfoDao rawMaterialInfoDao;
    @Autowired
    IProductionRecordDao recordDao;
    @Autowired
    IProductShipOrderDao shipOrderDao;
    @Override
    public Map<String,Object> queryList(Date time, String unit) {
        try {
            Map<String,Object> da = new HashMap<>();
            Calendar cal = Calendar.getInstance();
            cal.setTime(time);
            cal.add(Calendar.DATE, 1);
            var ss =  cal.getTime();
            Calendar cals = Calendar.getInstance();
            cals.setTime(time);
            cals.add(Calendar.MONDAY, 1);
            var sss =  cal.getTime();
            var endtime = Tl.strToDate(Tl.getDateString(ss, "yyyy-MM-dd 08:00"));
            var startime = Tl.strToDate(Tl.getDateString(time, "yyyy-MM-dd 08:00"));
            cals.set(cals.get(Calendar.YEAR),cals.get(Calendar.MONDAY),1,8,0);
            var endtimemonth = cals.getTime();
            cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY),1,8,0);
            var startimemonth = cal.getTime();
            var betons = HsApp.getSystemSet(CK.BetonSysCode);
            var result = costingDao.queryList(startime,endtime,unit);
            BigDecimal recordentity = shipOrderDao.queryMonthQuantity(unit, betons==null?"PCAT221014000001":betons,startime,endtime);
            for (ConsumptionModel entity : result) {
                 entity.setConsumptionQuantity(entity.getConsumptionQuantity().setScale(2,RoundingMode.HALF_UP));
                 entity.setPrice(rawMaterialInfoDao.getRMCostPrice(entity.getRmSn(),ss)==null? BigDecimal.valueOf(0) :rawMaterialInfoDao.getRMCostPrice(entity.getRmSn(),ss));
                 entity.setMoney(entity.getPrice().multiply(entity.getConsumptionQuantity()).setScale(2, RoundingMode.DOWN));
                 entity.setTotileValue((recordDao.queryTotile(entity.getRmSn(),unit).divide(BigDecimal.valueOf(1000))).setScale(2,RoundingMode.HALF_UP));
                 entity.setTotilemoney(entity.getTotileValue().multiply(entity.getPrice()));
                 entity.setDailyAveragePrice(entity.getMoney().divide(recordentity.compareTo(BigDecimal.valueOf(0))==0?BigDecimal.valueOf(1) :recordentity,2,RoundingMode.HALF_UP));
            }
            da.put("data",result);
            BigDecimal record = shipOrderDao.queryMonthQuantity(unit, betons==null?"PCAT221014000001":betons,startimemonth,endtimemonth);
            var results = costingDao.queryList(startimemonth,endtimemonth,unit);
            BigDecimal money = BigDecimal.valueOf(0.0);
            for (ConsumptionModel consumptionModel : results) {
                var dj = rawMaterialInfoDao.getRMCostPrice(consumptionModel.getRmSn(),ss);
                var sl = consumptionModel.getConsumptionQuantity()==null? BigDecimal.valueOf(0) :consumptionModel.getConsumptionQuantity();
                money.add(((dj==null? BigDecimal.valueOf(0) :dj).multiply(sl)).setScale(2, RoundingMode.DOWN));
            }
            da.put("yfl",record.setScale(2,RoundingMode.HALF_UP));
            da.put("fl",recordentity.setScale(2,RoundingMode.HALF_UP));
            da.put("junjia",(money.divide(record.compareTo(BigDecimal.valueOf(0))==0?BigDecimal.valueOf(1) :record)).setScale(2,RoundingMode.HALF_UP));
            return da;
        } catch (Exception ee) {
            throw  ee;
        }
    }
}
